﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Web;
using System.Linq;
using MediaStreamer.Containers;
using Org.Flowertwig.Media.Information.Services;

namespace MediaStreamer
{
    public class Client
    {
        public static void HandleRequest(object obj)
        {
            OnHandleRequest(obj as Socket);
        }

        public static void OnHandleRequest(Socket socket)
        {
            try
            {
                while (true)
                {
                    var buffer = new byte[1024];
                    var size = socket.Receive(buffer);
                    var responseInfo = PageHandler.HandleRequest(buffer, size);
                    Respond(socket, responseInfo);
                }
            }
            catch (ThreadAbortException)
            {
                try
                {
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                }
                catch (Exception) { }
            }
            catch (Exception)
            {
                Thread.CurrentThread.Abort();
            }
        }

        private static void Respond(Socket socket, ResponseInfo responseInfo)
        {
            var sb = new StringBuilder();

            switch (responseInfo.StatusCode)
            {
                case 200: sb.AppendLine("HTTP/1.1 200 OK"); break;
                case 304: sb.AppendLine("HTTP/1.1 304 Not Modified"); break;
                case 307: sb.AppendLine("HTTP/1.1 307 Temporary Redirect"); break;
                case 400: sb.AppendLine("HTTP/1.1 400 Bad Request"); break;
                case 401: sb.AppendLine("HTTP/1.1 401 Unauthorized"); break;
                case 402: sb.AppendLine("HTTP/1.1 402 Payment Required"); break;
                case 403: sb.AppendLine("HTTP/1.1 403 Forbidden"); break;
                case 404: sb.AppendLine("HTTP/1.1 404 Not Found"); break;
                case 405: sb.AppendLine("HTTP/1.1 405 Method Not Allowed"); break;
                case 406: sb.AppendLine("HTTP/1.1 406 Not Acceptable"); break;
                case 407: sb.AppendLine("HTTP/1.1 407 Proxy Authentication Required"); break;
                case 408: sb.AppendLine("HTTP/1.1 408 Request Timeout"); break;
                case 409: sb.AppendLine("HTTP/1.1 409 Conflict"); break;
            }

            //sb.AppendFormat("Date: {0:}" + DateTime.Now); // Mon, 23 May 2005 22:38:34 GMT
            //sb.AppendLine();

            sb.AppendLine("Server: FlowServ/2011.07.24");
            //sb.AppendLine("Connection: close");
            //sb.AppendLine("Content-Type: " + responseInfo.ContentType);
            sb.AppendLine("Content-Type: " + responseInfo.ContentType);
            sb.AppendFormat("Content-Length: {0}", responseInfo.Content != null ? responseInfo.Content.Length : 0);
            sb.AppendLine();
            sb.AppendLine();
            //sb.AppendLine();
            if (responseInfo.Content != null)
            {
                sb.Append(Encoding.UTF8.GetString(responseInfo.Content));
            }
            //sb.AppendLine();

            var data = Encoding.UTF8.GetBytes(sb.ToString());
            //System.Threading.Thread.Sleep(500);
            socket.Send(data);
        }

    }
}
